from prg.common.object_message import MessageBase


class UdpSend(MessageBase):
    def __init__(self, name, socket_item):
        super().__init__(name="udp_send_" + name)
        self.socket_item = socket_item
        self.local_socket = None
        self.read_max = None

    def run_send(self, read_max=1024):
        self.read_max = read_max
        self.start()

    def entry(self):
        with self.local_socket as local_socket:
            while self.is_run():
                if self.local_socket is not None:
                    read_byte_tmp, read_addr_tmp = local_socket.recvfrom(self.read_max)
                    if len(read_byte_tmp) > 0:
                        self.socket_item.send(read_byte_tmp)

